<!--
 * @Description: 01-求斐波那契数列
 * @Autor: wangxin
 * @Date: 2020-06-08 20:39:36
 * @LastEditors: Seven
 * @LastEditTime: 2020-06-08 20:58:44
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>求斐波那契数列</title>
  </head>
  <body>
    <script>
      /* 斐波那契数列前两项是1，从第三项开始，每一项都等于前两项之和 */

      /**
       * @description: 求斐波那契数列
       * @param {Number} num 数列长度
       * @return: {Array} 满足条件的斐波那契数列
       */
      function fibonacci(num) {
        const fibonacci = []
        fibonacci[1] = 1 // 斐波那契数列不存在0
        fibonacci[2] = 1

        for (let i = 3; i < num; i++) {
          fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]
        }

        return fibonacci
      }

      // 测试代码
      const list = fibonacci(20)
      console.log(list)
      // 1: 1
      // 2: 1
      // 3: 2
      // 4: 3
      // 5: 5
      // 6: 8
      // 7: 13
      // 8: 21
      // 9: 34
      // 10: 55
      // 11: 89
      // 12: 144
      // 13: 233
      // 14: 377
      // 15: 610
      // 16: 987
      // 17: 1597
      // 18: 2584
      // 19: 4181
    </script>
  </body>
</html>
